/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package expo43score;

import java.io.*;
import java.net.*;

/**
 *
 * @author TommySprat
 */
public class ClientThread extends Thread {

    Socket e43Socket = null;
    PrintWriter out = null;
    BufferedReader in = null;
    String HOST = "localhost";
    Expo43ScoreView myScoreView;
    
    public ClientThread(Expo43ScoreView masterScoreView){
        myScoreView = masterScoreView;
    }

    @Override
    public void run() {
        try {
            e43Socket = new Socket(HOST, 4445);
            out = new PrintWriter(e43Socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(e43Socket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: " + HOST);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: " + HOST);
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer;
        String toServer;

        try{
        while ((fromServer = in.readLine()) != null) {
            if (fromServer.equalsIgnoreCase("startscore")){
                myScoreView.setVisible(true);
                myScoreView.startCounting();
            }
            if (fromServer.equalsIgnoreCase("stopscore")){
                myScoreView.setVisible(false);
            }
            if (fromServer.equalsIgnoreCase("Bye.")) {
                break;
            }
            
        }
        } catch(IOException exc){
            System.out.println("DERP IOException");
            exc.printStackTrace();                    
        }
    }
}
